import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 20000 // request timeout
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    // 在发送请求之前做些什么
    if (store.getters.token) {
      //设置请求携带令牌
      config.headers['AUTHORIZATION'] = encodeURIComponent(getToken())
      config.headers['platform'] = 1
      // config.headers['Access-Control-Allow-Origin'] = '*'
      config.headers['P3P'] = "CP='IDC DSP COR CURa ADMa  OUR IND PHY ONL COM STA'"
      // config.headers['AUTHORIZATION'] = store.getters.token
    }
    return config
  },
  error => {
    // 处理请求错误
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  /**
   * 如果您想获取http信息，如头或状态,请返回response=>response
  */
  /**
    *通过自定义代码确定请求状态
    *这里只是一个例子
    *您还可以通过HTTP状态代码来判断状态
  */
  response => {
    // console.log('response', response);
    const res = response.data
    if (response.config.url == '/ajax/register/index' || response.config.url == '/ajax/ssq/check' || response.config.url == '/ajax/sign/checkSign' || response.config.url == '/ajax/index/getCityAjax' || response.config.url == '/ajax/ssq/checkSign' || response.config.url == '/ajax/withdraw/confirmWithdraw' || response.config.url == '/ajax/withdraw/buy' || response.config.url == '/ajax/recharge/buy' || response.config.url == '/ajax/recharge/quickbuy' || response.config.url == '/ajax/order/preCheck') {
      return res
    } else if (response.config.url == '/ajax/ssq/downloadContract ') {
      return res
    } else if (res.code === 1 || res.code === undefined || res.code === 10099 || res.code == 10044 || res.code == 20210 || res.code == 20220 || res.code == 20230) {
      return res
    } else if (res.code == 0 && res.msg == '未绑定银行卡' || res.msg == '已绑定银行卡') {
      return res
    } else if (res.code === 10099) {
      return Promise.reject(res || '网络太慢了，请重试')
    } else if (res.code === 10003) {
      let token = getToken()
      console.log('token10003', token);
      if (token) {
        // Message({
        //   // message: error.message,
        //   message: '登录状态异常，重新登录中...',
        //   type: 'error',
        //   duration: 2 * 1000
        // })
        window.location.reload();
      } else {
        // 前往登录
        console.log('token不存在了');
        Message({
          // message: error.message,
          message: '登录已失效，正在返回登录',
          type: 'error',
          duration: 2 * 1000
        })
        setTimeout(() => {
          // window.location.href = process.env.VUE_APP_BASE_SSO + '/sso/login/index'
          console.log('去登录', process.env.VUE_APP_BASE_SSO + '/sso/login/index?redirectUri=' + encodeURIComponent(window.location.href) + '&token_type=url');
          window.location.href = process.env.VUE_APP_BASE_SSO + '/sso/login/index?redirectUri=' + encodeURIComponent(window.location.href) + '&token_type=url'
          // http://test.mutouyun.com:9215/sso/login/indexhttp://test.mutouyun.com:9234/#/
        }, 1500);
        // return Promise.reject(res || 'Error')

      }
    } else {
      Message({
        message: res.msg || '网络太慢了，请重试',
        type: 'error',
        duration: 2 * 1000
      })
      console.log('调用接口失败了')
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      // message: error.message,
      message: ' 网络太慢了，请重试',
      type: 'error',
      duration: 2 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
